package com.meeting.meetingroom_api.dto;

import com.meeting.meetingroom_api.constants.ErrorMessages;
import com.meeting.meetingroom_api.enums.RoomStatus;
import com.meeting.meetingroom_api.enums.MeetingRoomType;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class MeetingRoomQueryDto {
    private String name; // Search by name (partial match)
    private MeetingRoomType type;
    private Integer minCapacity;
    //5个设备字段替换原来的不符合业务要求的单个字段
    private Boolean hasProjector;

    private Boolean hasSpeaker;

    private Boolean hasNetwork;

    private Boolean hasMicrophone;

    private Boolean hasWhiteboard;

    private RoomStatus status;

    private List<RoomStatus> statusList;
    // 自定义 setter：处理字符串参数和枚举列表参数
    public void setStatusList(Object statusList) {
        if (statusList == null) {
            this.statusList = Collections.emptyList();
            return;
        }

        // 处理字符串参数（如 "available,maintenance"）
        if (statusList instanceof String) {
            String statusStr = (String) statusList;
            if (statusStr.isEmpty()) {
                this.statusList = Collections.emptyList();
                return;
            }
            this.statusList = Arrays.stream(statusStr.split(","))
                    .map(s -> RoomStatus.valueOf(s.trim()))
                    .collect(Collectors.toList());
            return;
        }

        // 处理枚举列表参数（如 List<RoomStatus>）
        if (statusList instanceof List) {
            List<?> list = (List<?>) statusList;
            if (list.isEmpty()) {
                this.statusList = Collections.emptyList();
                return;
            }
            // 检查列表元素类型
            Object firstElement = list.get(0);
            if (firstElement instanceof RoomStatus) {
                //noinspection unchecked
                this.statusList = (List<RoomStatus>) list;
            } else if (firstElement instanceof String) {
                // 处理 List<String> 转换为 List<RoomStatus>
                this.statusList = list.stream()
                        .map(s -> RoomStatus.valueOf(((String) s).trim()))
                        .collect(Collectors.toList());
            } else {
                throw new IllegalArgumentException(String.format(ErrorMessages.UNSUPPORTED_STATUS_LIST_TYPE, firstElement.getClass()));
            }
            return;
        }

        throw new IllegalArgumentException(String.format(ErrorMessages.UNSUPPORTED_STATUS_PARAM_TYPE, statusList.getClass()));
    }

    // 自定义 getter：确保返回非空列表
    public List<RoomStatus> getStatusList() {
        return this.statusList == null ? Collections.emptyList() : this.statusList;
    }

    // For availability checks
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime desiredStartTime;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime desiredEndTime;

    // 分页参数
    //private Integer page = 0;
    //private Integer size = 10;
    // Pagination and Sorting (optional, can be handled by Pageable directly in controller)
    // private int page = 0;
    // private int size = 10;
    // private String sortBy = "roomId";
    // private String sortDir = "asc";
}